use crate::global_context::GlobalContext;
use std::sync::Mutex;
use tauri::{Manager, TitleBarStyle, WebviewUrl, WebviewWindowBuilder};
mod aviation_libs;
mod global_context;
mod tauri_utils;
mod unzip;
mod window_bar_control;

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_websocket::init())
        .plugin(tauri_plugin_os::init())
        .setup(|app| {
            let win_builder = WebviewWindowBuilder::new(app, "main", WebviewUrl::default())
                .inner_size(1200.0, 800.0)
                .title("OpenScope");

            #[cfg(target_os = "macos")]
            let win_builder = win_builder.title_bar_style(TitleBarStyle::Transparent);
            #[cfg(target_os = "windows")]
            let win_builder = win_builder.title_bar_style(TitleBarStyle::Overlay);

            let window = win_builder.build().unwrap();
            #[cfg(debug_assertions)] // 仅在调试构建时包含此代码
            {
                window.open_devtools();
                window.close_devtools();
            }
            app.manage(Mutex::new(GlobalContext {
                win_array: Vec::from([window]),
            }));
            Ok(())
        })
        .plugin(tauri_plugin_dialog::init())
        .plugin(tauri_plugin_fs::init())
        .plugin(tauri_plugin_log::Builder::new().build())
        .plugin(tauri_plugin_notification::init())
        .plugin(tauri_plugin_http::init())
        .plugin(tauri_plugin_opener::init())
        .invoke_handler(tauri::generate_handler![
            aviation_libs::get_metar,
            window_bar_control::set_bar_color,
            unzip::unzip_uri,
            tauri_utils::create_window,
            tauri_utils::open_path
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
